package com.online.fund;

import java.io.IOException;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import com.online.fund.util.HttpClientUtil;

import jdk.nashorn.api.scripting.ScriptObjectMirror;

@SuppressWarnings("restriction")
public class Java8Tester {
	public static void main(String args[]) throws IOException {
		ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
		ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");
		String name = "Mahesh";
		String fun = "var x = {'code': 123}";
		String resultStr = HttpClientUtil.httpGetRequest(
				"http://fund.eastmoney.com/Data/Fund_JJJZ_Data.aspx?t=1&lx=1&letter=&gsid=&text=&sort=zdf,desc&page=1,2&dt=1516200330479&atfc=&onlySale=0");
		System.out.println(resultStr);

		Integer result = null;
		try {
			nashorn.eval("print('" + name + "')");
			result = (Integer) nashorn.eval("10 + 2");
			nashorn.eval(fun);
			ScriptObjectMirror x = (ScriptObjectMirror) nashorn.get("x");
			System.out.println(x.get("code"));

			nashorn.eval(resultStr);
			ScriptObjectMirror db = (ScriptObjectMirror) nashorn.get("db");
			ScriptObjectMirror datas = (ScriptObjectMirror) db.get("datas");
			if (datas.isArray()) {
				for (int i = 0; i < datas.size(); i++) {
					ScriptObjectMirror data = (ScriptObjectMirror)datas.get("" + i);
					if (data.isArray()) {
						for (int j = 0; j < data.size(); j++) {
							System.out.println(data.get("" + j));
						}
//						String[] dataStr = (String[]) ScriptUtils.convert(data, String[].class);
//						System.out.println(dataStr);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error executing script: " + e.getMessage());
		}
		System.out.println(result.toString());
	}
}